﻿using System;
using Microsoft.Extensions.Configuration;

namespace USERS.CYT369.COM.UTILITY
{
    public class SystemSettingService
    {
        #region init
        private static readonly SystemSettingService m_Instance = new SystemSettingService();
        private SystemSettingService()
        {
#if DEBUG
            string basePath = System.IO.Directory.GetCurrentDirectory();
            if (!System.IO.File.Exists(System.IO.Path.Combine(basePath, "appsettings.json")))
            {
                basePath = new System.IO.FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location).DirectoryName;
            }
#else
            string basePath = new System.IO.FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location).DirectoryName;
#endif
            var builder = new ConfigurationBuilder()
                .SetBasePath(basePath).AddJsonFile(@"appsettings.json", optional: true, reloadOnChange: true);

            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; set; }

        /// <summary>
        /// 类型实例
        /// </summary>
        public static SystemSettingService Instance
        {
            get
            {
                return m_Instance;
            }
        }
        #endregion

        #region Connection
        public string RedisDB
        {
            get
            {
                return Configuration.GetSection("DBConnectionStrings:RedisDB:ConnectionString").Value;
            }
        }

        private Connection m_UsersDB;
        public Connection UsersDB
        {
            get
            {
                if (m_UsersDB == null)
                {
                    m_UsersDB = new Connection()
                    {
                        DBType = Configuration.GetSection("DBConnectionStrings:UsersDB:DBType").Value,
                        DBSchema = Configuration.GetSection("DBConnectionStrings:UsersDB:Schema").Value,
                        ConnectionString = Configuration.GetSection("DBConnectionStrings:UsersDB:ConnectionString").Value
                    };
                }

                return m_UsersDB;
            }
        }

        private Connection m_NewsDB;
        public Connection NewsDB
        {
            get
            {
                if (m_NewsDB == null)
                {
                    m_NewsDB = new Connection()
                    {
                        DBType = Configuration.GetSection("DBConnectionStrings:NewsDB:DBType").Value,
                        DBSchema = Configuration.GetSection("DBConnectionStrings:NewsDB:Schema").Value,
                        ConnectionString = Configuration.GetSection("DBConnectionStrings:NewsDB:ConnectionString").Value
                    };
                }

                return m_NewsDB;
            }
        }

        private Connection m_BrandDB;
        public Connection BrandDB
        {
            get
            {
                if (m_BrandDB == null)
                {
                    m_BrandDB = new Connection()
                    {
                        DBType = Configuration.GetSection("DBConnectionStrings:BrandDB:DBType").Value,
                        DBSchema = Configuration.GetSection("DBConnectionStrings:BrandDB:Schema").Value,
                        ConnectionString = Configuration.GetSection("DBConnectionStrings:BrandDB:ConnectionString").Value
                    };
                }

                return m_BrandDB;
            }
        }

        private Connection m_BaseDB;
        public Connection BaseDB
        {
            get
            {
                if (m_BaseDB == null)
                {
                    m_BaseDB = new Connection()
                    {
                        DBType = Configuration.GetSection("DBConnectionStrings:BaseDB:DBType").Value,
                        DBSchema = Configuration.GetSection("DBConnectionStrings:BaseDB:Schema").Value,
                        ConnectionString = Configuration.GetSection("DBConnectionStrings:BaseDB:ConnectionString").Value
                    };
                }

                return m_BaseDB;
            }
        }
        #endregion

        #region SystemSetting

        public string SystemName
        {
            get
            {
                return Configuration.GetSection("SystemSetting:SystemName").Value;
            }
        }

        public int PageSize
        {
            get
            {
                int id = 0;
                int.TryParse(Configuration.GetSection("SystemSetting:PageSize").Value, out id);
                if (id <= 0)
                {
                    id = 20;
                }
                return id;
            }
        }

        public string PageList
        {
            get
            {
                return Configuration.GetSection("SystemSetting:PageList").Value;
            }
        }

        /// <summary>
        /// 登录超时时间
        /// </summary>
        public int IdleTimeout
        {
            get
            {
                int n = 0;
                int.TryParse(Configuration.GetSection("SystemSetting:IdleTimeout").Value, out n);
                if (n <= 0)
                {
                    n = 120;
                }
                return n;
            }
        }

        /// <summary>
        /// 验证码超时时间(秒)
        /// </summary>
        public int CaptchaTimeout
        {
            get
            {
                int n = 0;
                int.TryParse(Configuration.GetSection("SystemSetting:CaptchaTimeout").Value, out n);
                if (n <= 0)
                {
                    n = 120;
                }
                return n;
            }
        }


        public string DesKV
        {
            get
            {
                return Configuration.GetSection("SystemSetting:DesKV").Value;
            }
        }
        public string DesIV
        {
            get
            {
                return Configuration.GetSection("SystemSetting:DesIV").Value;
            }
        }
        public string LoginDefaultRedirect
        {
            get
            {
                return Configuration.GetSection("SystemSetting:LoginDefaultRedirect").Value;
            }
        }
        public string QQCallback
        {
            get
            {
                return Configuration.GetSection("SystemSetting:QQCallback").Value;
            }
        }
        

        public string NewsServer
        {
            get
            {
                return Configuration.GetSection("SystemSetting:NewsServer").Value;
            }
        }
        public string BrandServer
        {
            get
            {
                return Configuration.GetSection("SystemSetting:NewsServer").Value;
            
            }
        }
        #endregion

        #region path
        public string LoginPath
        {
            get
            {
                var path = Configuration.GetSection("AppPath:LoginPath").Value;
                return path;
            }
        }

        #endregion


        #region Aliyun.OSS
        private USERS.CYT369.COM.UTILITY.Aliyuns.EndpointParameter m_NewsInternalBucket;

        /// <summary>
        /// 内容管理系统图片/文件库
        /// </summary>
        public USERS.CYT369.COM.UTILITY.Aliyuns.EndpointParameter NewsInternalBucket
        {
            get
            {
                if (m_NewsInternalBucket == null)
                {
                    m_NewsInternalBucket = new USERS.CYT369.COM.UTILITY.Aliyuns.EndpointParameter()
                    {
                        Endpoint = Configuration.GetSection("Aliyun.OSS:News:Internal").Value,
                        AccessKeyId = Configuration.GetSection("Aliyun.OSS:News:AccessKeyId").Value,
                        AccessKeySecret = Configuration.GetSection("Aliyun.OSS:News:AccessKeySecret").Value,
                        Bucket = Configuration.GetSection("Aliyun.OSS:News:Public.Bucket").Value,
                        HttpURL = Configuration.GetSection("Aliyun.OSS:News:Public.Bucket.HTTP").Value
                    };
                }

                return m_NewsInternalBucket;
            }
        }

        private USERS.CYT369.COM.UTILITY.Aliyuns.EndpointParameter m_BrandInternalBucket;

        /// <summary>
        /// 内容管理系统图片/文件库
        /// </summary>
        public USERS.CYT369.COM.UTILITY.Aliyuns.EndpointParameter BrandInternalBucket
        {
            get
            {
                if (m_BrandInternalBucket == null)
                {
                    m_BrandInternalBucket = new USERS.CYT369.COM.UTILITY.Aliyuns.EndpointParameter()
                    {
                        Endpoint = Configuration.GetSection("Aliyun.OSS:Brand:Internal").Value,
                        AccessKeyId = Configuration.GetSection("Aliyun.OSS:Brand:AccessKeyId").Value,
                        AccessKeySecret = Configuration.GetSection("Aliyun.OSS:Brand:AccessKeySecret").Value,
                        Bucket = Configuration.GetSection("Aliyun.OSS:Brand:Public.Bucket").Value,
                        HttpURL = Configuration.GetSection("Aliyun.OSS:Brand:Public.Bucket.HTTP").Value
                    };
                }

                return m_BrandInternalBucket;
            }
        }
        #endregion

        #region Audience
        public string Audience_Secret
        {
            get
            {
                return Configuration.GetSection("Audience:Secret").Value;
            }
        }

        public string Audience_Iss
        {
            get
            {
                return Configuration.GetSection("Audience:Iss").Value;
            }
        }

        public string Audience_Aud
        {
            get
            {
                return Configuration.GetSection("Audience:Aud").Value;
            }
        }

        #endregion
    }


}
